package qdu.rg012.ch4_data_contntprvdr;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

public class Myctntprvdr extends ContentProvider {
    Mydbapt mydbapt;

    //-------------URI--------------------------
    static String uristr = "content://qdu.rg012.ch4_data_contntprvdr";
    static String multi_path = "/mydata";
    static String item_path = "/mydata/#";
    /*
     * 1. URI格式中，协议需要为content://
     *              host需要与清单中注册的authority匹配
     * 2. 当前uristr字符串相当于制定了contentprovider的数据库
     * 3. uristr+multi_path相当于对应了contentprovider中叫做mydata的表格
     * 4. uristr+item_path相当于mydata表格中的某一条记录。#时通配符，代表行号。
     * 5. provider中重写的gettype函数需要为（uristr+multi_path/uristr+item_path）
     *    URI格式自定义相应的MIME类型
     *
     * 6. MIME类型 格式： 类型 / 子类型
     *            例如:  text/html  video/mp4  image/jpeg 等等
     */

    //------------------重写回调------------------
    @Override
    public boolean onCreate() {
        mydbapt = new Mydbapt(this.getContext());
        return mydbapt.opendb();
//        if(mydbapt != null && mydbapt.dbisopen())
//            return true;
//        else
//            return false;
    }
    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        if((uristr+multi_path).equals(uri.toString()))
            return "vnd.android.cursor.dir/mymsg";
        else if((uristr+item_path).equals(uri.toString()))
            return "vnd.android.cursor.item/mymsg";
        else
            return null;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        //Log.i("mytag", "provider selection:" + selection + " uri:" + uri.toString());
        if(mydbapt != null && mydbapt.dbisopen()){
            //Log.i("mytag", "provider query dbapt ok uristr+multi_path+\"/\": " + uristr+multi_path+"/");
            if((uristr+multi_path).equals(uri.toString()) && selection == null){
                Log.i("mytag", "provider getall");
                return mydbapt.getall();
            }else if((uristr+multi_path+"/").equals(uri.toString()) && selection!= null && !"".equals(selection)){
                //Log.i("mytag", "provider get by id :" + selection);
                return mydbapt.getitembyid(selection);
            }else
                return null;
        }else
            return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        long newid = -1;
        Uri newitem = null;
        if(mydbapt != null && mydbapt.dbisopen()){
            if((uristr+multi_path).equals(uri.toString())){
                newid = mydbapt.insertitem(values);
                if(newid > 0){
                    newitem = Uri.parse(uristr + multi_path + "/" + newid);
                    //为新生成的数据准备相应的Uri，最为return的值
                }
            }
        }
        return newitem;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }
}
